<?php
/**
 * User: aka
 * Date: 2018/6/24
 * Time: 15:11
 */

namespace app\common\hash;


class EncodeOperation
{
    static private $saltCode = 'abcdefghijklmnopqrstuvwxyz0123456789';

    public function getSalt(int $saltlength) : string
    {
        return $this->setSalt($saltlength);
    }

    public function getEncodePassword(string $salt,string $password,string $key) : string
    {
        return $this->setEncodePassword($salt,$password,$key);
    }

    public function getEncodeEmail(string $email,string $key) : string
    {
        return $this->setEncodeEmail($email,$key);
    }

    private function setEncodeEmail(string $email,string $key) : string
    {
        $md5Email = md5($email);
        $md5Key = md5($key);
        $encodeEmail = md5($md5Email.$md5Key);
        return $encodeEmail;
    }

    private function setEncodePassword(string $salt,string $password,string $key) : string
    {
        $md5Password = md5($password);
        $md5Salt = md5($salt);
        $md5Key = md5($key);
        $encodePassword = md5($md5Password.$md5Salt.$md5Key);
        return $encodePassword;
    }

    private function setSalt(int $saltlength) : string
    {
        $chars = self::$saltCode;
        $str ="";
        for ( $i = 0; $i < $saltlength; $i++ ) {
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }
}